package com.zmx.b2c.order.messaging.listener;

import com.zmx.b2c.order.entity.B2COrder;
import com.zmx.b2c.order.entity.B2COrderLog;
import com.zmx.b2c.order.entity.B2COrdercancel;
import com.zmx.b2c.order.repository.B2COrderLogRepository;
import com.zmx.b2c.order.repository.B2COrderRepository;
import com.zmx.b2c.order.repository.B2COrdercancelRepository;
import com.zmx.b2c.order.service.B2COrderService;
import com.zmx.common.common.contents.MqQueueContents;
import com.zmx.common.common.enums.Enum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @Description: 超时未支付订单消息队列监听类
 * @ClassName: TimeOutOrderListener
 * @Author zhaomxr
 * @Date 2021-10-22 15:54
 */
@Slf4j
@Component
public class TimeOutOrderListener {

    @Autowired
    private B2COrderService b2COrderService;
    @Autowired
    private B2COrderRepository b2COrderRepository;
    @Autowired
    private B2COrdercancelRepository b2COrdercancelRepository;
    @Autowired
    private B2COrderLogRepository b2COrderLogRepository;
    @Autowired
    private TimeOutOrderListener timeOutOrderListener;

    /**
     * 消费超时未支付订单队列
     * @param orderCode
     */
    @RabbitHandler
    @RabbitListener(queues = MqQueueContents.ORDER_TIMEOUT_DLX_QUEUE)
    public void onMessage(String orderCode) {
        log.info("取消未支付订单 订单code：{}", orderCode);

        B2COrder b2COrder = b2COrderRepository.findByOrderSn(orderCode);
        if (b2COrder != null && Objects.equals(Enum.PaymentStatus.unpaid.getCode(), b2COrder.getPaymentStatus())) {
            cacelOrder(b2COrder);
            log.info("订单【{}】已取消", b2COrder.getOrderSn());
        } else {
            log.info("订单【{}】支付状态错误 支付状态【{}】", b2COrder.getOrderSn(), Enum.PaymentStatus.getPaymentStatusByCode(b2COrder.getPaymentStatus()).getMessage());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void cacelOrder(B2COrder b2COrder) {
        //记录订单取消队列
        B2COrdercancel cancelOrder = new B2COrdercancel();
        cancelOrder.setId(b2COrder.getId());
        cancelOrder.setStatus(1);
        cancelOrder.setOrders(b2COrder.getId());
        cancelOrder.setOrderSn(b2COrder.getOrderSn());
        cancelOrder.setCreateTime(new Date());
        cancelOrder.setCreator("sys_cancel");
        cancelOrder.setSource(2);
        cancelOrder.setReason("no pay than 30 minute");
        cancelOrder.setTs(new Date());
        cancelOrder.setDr(0);
        b2COrdercancelRepository.save(cancelOrder);

        //记录取消订单日志
        timeOutOrderListener.saveOrderLog(b2COrder);

        //修改订单状态为已取消
        b2COrderService.updateOrderStatusByCode(b2COrder.getOrderSn(), Enum.OrderStatus.canceled);
    }

    /**
     * 记录取消订单日志
     */
    @Async
    public void saveOrderLog(B2COrder b2COrder) {
        B2COrderLog b2COrderLog = new B2COrderLog();
        b2COrderLog.setOrders(b2COrder.getId());
        b2COrderLog.setType(0);
        b2COrderLog.setOperator("sys_cancel");
        b2COrderLog.setContent("no pay than 30 minute");
        b2COrderLog.setCreateTime(new Date());
        b2COrderLog.setModifyTime(new Date());
        b2COrderLog.setDr(0);
        b2COrderLog.setTs(new Date());
        b2COrderLogRepository.save(b2COrderLog);
    }
}
